package com.example.springback.service;

import com.example.springback.domain.Admin;
import com.example.springback.exception.CustomException;
import com.example.springback.mapper.AdminMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminService {
    @Resource
    AdminMapper adminMapper;
    public PageInfo<Admin> selectPage(Admin admin, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        List<Admin> admins=adminMapper.selectAll(admin);
        return PageInfo.of(admins);
    }

    public void add(Admin admin) {
        if(admin==null){
            throw new CustomException("500","管理员信息为空");
        }
        Admin admin1=adminMapper.selectByUsername(admin.getUsername());
        if(admin1!=null){
            throw new CustomException("500","账号已存在");
        }
        adminMapper.insert(admin);
    }

    public void update(Admin admin) {
        adminMapper.updateByPrimaryKey(admin);

    }

    public void delete(Integer adminId) {
        adminMapper.deleteByPrimaryKey(adminId);
    }

    public Admin login(Admin admin) {
        String  username=admin.getUsername();//管理员编号
        Admin admin1=adminMapper.selectByUsername(username);
        if (admin1==null){//没查到
            throw new CustomException("500","账号不存在");
        }
        //账号存在
        String password=admin.getPassword();
        if (!admin1.getPassword().equals(password)){//不匹配
            throw new CustomException("500","账号或密码错误");
        }
        return admin1;
    }
}
